/*
 * @lc app=leetcode.cn id=541 lang=csharp
 *
 * [541] 反转字符串 II
 */

// @lc code=start
public class Solution {
    public string ReverseStr(string s, int k) {
        char[] chars = s.ToCharArray();
        int left, right = 0;
        var ix = 0;
        var len = s.Length;
        
        while (ix < len)
        {
            left = ix;
            right = Math.Min(len - 1, ix + k - 1);
            ix += k << 1;
            
            while (left < right)
            {
                var temp = chars[left];
                chars[left] = chars[right];
                chars[right] = temp;
                ++left;
                --right;
            }
        }
        
        return new string(chars);
    }
}
// @lc code=end

